Debunking C++ Myths by Alexandru Bolboacă | Ferenc-Lajos Deák
Author:Alexandru Bolboacă | Ferenc-Lajos Deák
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2024-11-25T00:00:00+00:00
The dark orders of C++
There is one dark corner of the C++ language that is rarely touched by sunlight, and if a piece of code from these depths surfaces by any chance, a gang of hardcore developers immediately jumps on it and refactors it into digestible bits and bytes. Letâs consider, for example, the very simple case of why the a[2] and 2[a] expressions are equivalent when in C++, and a is an array of objects:
int main() { int a[16] = {0}; a[2] = 3; 3[a] = 4; }
The preceding piece of code, despite the fact that it looks ugly, actually compiles. The reason is the following: in C++, the operator [] array subscript is defined in terms of pointer arithmetic. The a[i] expression is translated by the compiler into *(a + i) , where a is a pointer to the first element of the array and i is the index. The i[a] expression at the end is also translated to the *(i + a) expression, where i is the index and a is the pointer to our arraysâ first elements.
Since addition is commutative for the compiler, it does not really matter which comes first.
So, we have found a specific case where order doesnât really matter in C++. But this works only for old-style C arrays; std::vector and std::array do not accept this kind of out-of-order syntax. There is a very specific reasoning for that; the subscript operator for std::vector and std::array does not support the commutative behavior seen in raw arrays, namely the following:
Operator overloading : The operator[] for std::vector and std::array is a member function, meaning it needs to be called on an instance of the class. It cannot be invoked with the index first, as member functions require the object to be on the left side of the call.
No pointer arithmetic : The internal implementation of std::vector and std::array does not rely on raw pointer arithmetic for indexing. They manage their memory and bounds checking differently, ensuring safer access to elements.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(214769)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(102521)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(102046)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(101838)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74619)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(51002)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40374)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40296)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40208)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32814)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32611)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32530)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32468)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32443)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32405)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32335)
What's Done in Darkness by Kayla Perrin(27259)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26631)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26555)